package cn.linweijian.kafka.producerdemo.service;

import cn.linweijian.kafka.producerdemo.vo.Article;
import cn.linweijian.kafka.producerdemo.vo.MessageVo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;




/**
 * Description: kafkademo
 * <p>
 * Created by linweijian on 2018/5/31 11:43
 */
@Component
public class SendService {
    private final Logger log= LoggerFactory.getLogger(SendService.class);
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    private Gson gson = new GsonBuilder().create();

    /**
     * 发送方法
     * @author linweijian
     * @date 2018/5/31 11:46
     * @param
     * @return void
     **/
    public void send(Object o) {
       Article article=(Article)o;
       article.setId(String.valueOf(System.currentTimeMillis()));
        log.info("+++++++++++++++++++++  message = {}", gson.toJson(article));
        kafkaTemplate.send("zhuti1","key", gson.toJson(article));

    }
}
